$(function () {
  $(".btnclick").click(function () {
    if ($(".orderdetails").hasClass("morebox")) {
      $(".orderdetails").removeClass("morebox").addClass("lessbox");
      $(this).val("订单详情+");
    } else {
      $(".orderdetails").removeClass("lessbox").addClass("morebox");
      $(this).val("订单详情-");
    }
  });
});


//修改确认支付按钮的背景
function updateBtn(isShow) {
  if (isShow) {
    $("#comfirmPayBtn").parent(".mart10").css("display", "");
    $("#grayBtn").parent(".mart10").css("display", "none");
  } else {
    $("#comfirmPayBtn").parent(".mart10").css("display", "none");
    $("#grayBtn").parent(".mart10").css("display", "");
  }
}

//获取支付要素的初始值
var mobileId = $("#mobileId").val();
var identityCode = $("#identityCode").val();//加密的身份证
var cardHolder = $("#cardHolder").val();
var cvv2 = $("#cvv2").val();
var cardId = $("#cardId").val();
var validDateM = $("#validDateM").val();
var validDateY = $("#validDateY").val();

var wait = 60;
function time(o) {
  if (wait == 0) {
    o.removeAttribute("disabled");
    o.style.cursor = 'pointer';
    o.value = "重新获取";
    o.className = "graybtn";
    wait = 60;
  } else if (wait == 60) {
    o.className = "graybtnno";
    o.setAttribute("disabled", true);
    o.style.cursor = 'default';
    o.value = wait + "秒后点击重新发送";
    var url = "";
    try {
      //判断是否为协议支付
      var pAgreementId = $("#pAgreementId").val();
      var ubAgreementId = $("#ubAgreementId").val();
      var ajaxSignSource = $("#ajaxSignSource").val();
      var productId = $("#productId").val();
      var bProductId = $("#bProductId").val();
      var isPAgreementPay = $("#isPAgreementPay").val();
      if ('Y' == isPAgreementPay) {//如果为协议支付，不需要进行实名认证
        url = $("#shortCutAjaxVerifyCodeSmsUrl").val();
        url = url + "?mobileId=" + mobileId + "&trace=" + trace
          + "&instId=" + instId + "&rpid=" + rpid + "&orderDate=" + orderDate + "&orderId=" + orderId + "&merId=" + merId
          + "&amount=" + amount + "&merName=" + merName + "&isPAgreementPay=" + isPAgreementPay + "&ajaxSignSource=" + ajaxSignSource + "&pAgreementId=" + pAgreementId + "&ubAgreementId=" + ubAgreementId + "&productId=" + productId + "&bProductId=" + bProductId;
      } else {
        // 校验支付要素输入是否都传入并合法
        var error = checkElementAndSetRsa();

        if (error) {
          o.removeAttribute("disabled");
          o.style.cursor = 'pointer';
          o.value = "重新获取";
          o.className = "graybtn";
          wait = 60;
          return;
        }
        //alert("randomNum:"+randomNum);

        //设置隐藏域中支付要素的值
        if (!("B007" == binBankId)) {//非招行，生成身份证号的加密串
          _$("#identityCode").sgAttr.attr({ t: "value", v: identityCode });
          _$("#cardHolder").sgAttr.attr({ t: "value", v: cardHolder });
        }
        _$("#cvv2").sgAttr.attr({ t: "value", v: cvv2 });
        _$("#cardId").sgAttr.attr({ t: "value", v: cardId });
        _$("#mobileId").sgAttr.attr({ t: "value", v: mobileId });
        $("#validDateY").val(validDateY);
        $("#validDateM").val(validDateM);

        url = $("#shortCutAjaxVerifyCodeSmsUrl").val();
        url = url + "?mobileId=" + mobileId + "&trace=" + trace
          + "&instId=" + instId + "&rpid=" + rpid + "&orderDate="
          + orderDate + "&amount=" + amount + "&merName=" + merName + "&orderId=" + orderId + "&merId=" + merId
          + "&cardId=" + encodeURIComponent(cardId) + "&identityCode=" + encodeURIComponent(identityCode) + "&cvv2="
          + encodeURIComponent(cvv2) + "&validDateM=" + validDateM + "&validDateY=" + validDateY + "&merId=" + merId + "&isPAgreementPay=" + isPAgreementPay + "&ajaxSignSource=" + ajaxSignSource + "&pAgreementId=" + pAgreementId + "&ubAgreementId=" + ubAgreementId + "&productId=" + productId + "&bProductId=" + bProductId + "&cardHolder=" + encodeURIComponent(cardHolder) + "&binBankId=" + binBankId;

      }
      verifyCodeSms(url, o);
      wait--;
      time(o);
    } catch (e) {
      o.removeAttribute("disabled");
      o.style.cursor = 'pointer';
      o.value = "重新获取";
      o.className = "graybtn";
      wait = 60;
      alert(e);
      //alert("客户端没有正确安装，请下载安装后再试！");
      return false;
    }
  } else {
    o.className = "graybtnno";
    o.setAttribute("disabled", true);
    o.style.cursor = 'default';
    o.value = wait + "秒后点击重新发送";
    wait--;
    setTimeout(function () {
      time(o);
    }, 1000);
  }
}
//检查各支付要素输入是否合法，如果合法，则加密
function checkElementAndSetRsa() {
  var error = false;
  var modifyFlag = $("#modifyFlag").val();
  var mobileObj = $("#mobileIdV");
  if (mobileObj.val() != null) {//如果为不允许修改的情况，将不存在mobileIdV元素
    var mobileIdV = mobileObj.val();
    if (mobileIdV == "") {
      $("#mobileErr").show();
      error = true;
    } else if (mobileIdV != mobileObj.attr("rel") && !(_$().checkMobileId(mobileIdV))) {
      $("#mobileErr").show();
      error = true;
    } else if (mobileIdV == mobileObj.attr("rel")) {
      $("#mobileErr").hide();
    } else if (mobileIdV != mobileObj.attr("rel") && (_$().checkMobileId(mobileIdV))) {
      mobileId = mobileIdV;
      $("#mobileErr").hide();
    }
  }

  if ("P0050000" == baseProductId) {//信用卡时才校验有效期
    if ($("#validDateMV").val() != null && $("#validDateYV").val() != null) {
      var month = doc.getElementById("validDateMV").value, year = doc.getElementById("validDateYV").value;
      if (month == "" || year == "") {
        error = true;
        $("#validDateErr").show();
      } else {
        validDateM = month;
        validDateY = year;
        $("#validDateErr").hide();
      }
    }
  }

  // 如果不是招行，验证身份证
  var binBankId = _$("#binBankId").sgAttr.attr({ t: "value" });

  if ($("#identityCodeV").val() != null) {
    var identityCodeV = $("#identityCodeV").val();//展示的身份证
    var identiryCodeRel = $("#identityCodeV").attr("rel");//初始值
    if (!("B007" == binBankId)) {
      if (identityCodeV == "") {//如果没有默认值，并且没有输入
        $("#identityErr").hide();
        $("#identityEmpty").show();
        error = true;
      } else if (identityCodeV != identiryCodeRel && !(checkIdcard(identityCodeV))) {//如果不为带*的默认值，并且输入不合法
        $("#identityEmpty").hide();
        $("#identityErr").show();
        error = true;
      } else if (identityCodeV == identiryCodeRel) {
        $("#identityEmpty").hide();
        $("#identityErr").hide();
      } else if (identityCodeV != identiryCodeRel && (checkIdcard(identityCodeV))) {
        identityCode = doRSAEncrypt(identityCodeV);//重新使用证书加密
        $("#identityEmpty").hide();
        $("#identityErr").hide();
      }
    }
  }

  if ($("#cardHolderV").val() != null) {
    var cardHolderV = $("#cardHolderV").val();
    var cardHolderRel = $("#cardHolderV").attr("rel");
    if (!("B007" == binBankId)) {//如果不是招行，验证姓名
      if (cardHolderV == "" || (cardHolderV != cardHolderRel && !(_$().checkChinese(cardHolderV)))) {
        $("#cardHolderErr").show();
        error = true;
      } else if (cardHolderV == cardHolderRel) {
        $("#cardHolderErr").hide();
      } else if (cardHolderV != cardHolderRel && (_$().checkChinese(cardHolderV))) {
        cardHolder = cardHolderV;//身份证不加密
        $("#cardHolderErr").hide();
      }
    }
  }

  if ("P0050000" == baseProductId) {//信用卡时才校验cvv2
    if ($("#cvv2V").val() != null) {
      var cvv2V = $("#cvv2V").val();
      var cvv2Rel = $("#cvv2V").attr("rel");
      if (cvv2V == "" || (cvv2V != cvv2Rel && !(checkCVV2(cvv2V)))) {
        $("#cvv2Err").show();
        error = true;
      } else if (cvv2V == cvv2) {
        $("#cvv2Err").hide();
      } else if (cvv2V != cvv2Rel && (checkCVV2(cvv2V))) {
        cvv2 = doRSAEncrypt(cvv2V);//重新使用证书加密
        $("#cvv2Err").hide();
      }
    }
  }

  if ($("#cardIdV").val() != null) {
    var cardIdV = $("#cardIdV").val();
    cardIdV = cardIdV.replace(/\s/g, '');
    var cardIdRel = $("#cardIdV").attr("rel");
    if (cardIdV == "") {
      $("#cardErr").hide();
      $("#cardEmpty").show();
      error = true;
    } else if (cardIdV != cardIdRel && !(checkCreditCard(cardIdV))) {
      $("#cardEmpty").hide();
      $("#cardErr").show();
      error = true;
    } else if (cardIdV == cardIdRel) {
      $("#cardErr").hide();
      $("#cardEmpty").hide();
    } else if (cardIdV != cardIdRel && (checkCreditCard(cardIdV))) {
      cardId = doRSAEncrypt(cardIdV);//重新使用证书加密
      $("#cardErr").hide();
      $("#cardEmpty").hide();
    }
  }
  return error;
}

function verifyCodeSms(url, o) {
  _$().reqAjax(
    url,
    function (result) {
      var backInf = eval('(' + result + ')');
      /*var regTrace = backInf.regTrace;
      if(typeof(regTrace) != "undefined" && regTrace.length>0){
        _$("#regTrace").sgAttr.attr({t:"value",v:backInf.regTrace});
      }*/
      if (backInf.retCode == "40017") { // 短信条数下发超限
        $("#msgVerifyErr>span").html(
          "短信验证码下发次数已达上限.<br />您可以选择其它支付方式，或返回商家重新下单.");
        $("#msgVerifyErr").show();
      } else if (backInf.retCode == "80707") {
        $("#msgVerifyErr>span").html("同一订单号请求验证码次数超过最大次数.");
        $("#msgVerifyErr").show();
      } else if (backInf.retCode == "80706") {
        $("#msgVerifyErr>span").html("验证码一分钟内获取多次,请稍后再试,谢谢!");
        o.removeAttribute("disabled");
        o.style.cursor = 'pointer';
        o.value = "重新获取";
        o.className = "graybtn";
        $("#msgVerifyErr").show();
        $("#verifyCodeErr").hide();
      } else if ((backInf.retCode == "60799")) {//实名认证不通过
        $("#msgVerifyErr>span").html("您输入的卡信息有误，请核对后重新获取验证码");
        o.removeAttribute("disabled");
        o.style.cursor = 'pointer';
        o.value = "重新获取";
        o.className = "graybtn";
        $("#msgVerifyErr").show();
        $("#verifyCodeErr").hide();
        wait = 0;
      } else if (!(backInf.retCode == "0")) {
        var retMsg = backInf.retMsg;
        $("#msgVerifyErr>span").html(retMsg);
        o.removeAttribute("disabled");
        o.style.cursor = 'pointer';
        o.value = "重新获取";
        o.className = "graybtn";
        if (typeof (retMsg) != "undefined" && retMsg.length > 0) {
          $("#verifyCodeErr").hide();
          $("#msgVerifyErr").show();
        } else {
          $("#verifyCodeErr").show();
          $("#msgVerifyErr").hide();
        }
        wait = 0;
      } else {
        //_$("#randomNum").sgAttr.attr({t:"value",v:backInf.randomNum});
        $("#verifyCodeErr").hide();
        $("#msgVerifyErr").hide();
      }
    });
}

// 当用户点击确认支付时，先ajax校验短信验证码，然后使用js提交表单确认支付
function verifyCodeValidate() {
  updateBtn(false);
  var error = false;
  try {
    var smsVerifyCode = $("#verifyCodeV");
    if (smsVerifyCode.val() == null || smsVerifyCode.val() == "" || smsVerifyCode.val() == smsVerifyCode.attr("rel")) {
      $("#verifyCodeErr").show();
      error = true;
    } else if (!(_$().checkVerCode(smsVerifyCode.val()))) {
      $("#verifyCodeErr").show();
      error = true;
    } else {
      $("#verifyCodeErr").hide();
      $("#msgVerifyErr").hide();
    }
    var mobileObj = $("#mobileId");
    var binBankId = _$("#binBankId").sgAttr.attr({ t: "value" });
    var pAgreementId = $("#pAgreementId").val();
    var ubAgreementId = $("#ubAgreementId").val();
    var isPAgreementPay = $("#isPAgreementPay").val();
    var productId = $("#productId").val();
    var bProductId = $("#bProductId").val();
    var baseProductId = $("#baseProductId").val();
    var merCustId = $("#merCustId").val();
    var servPriv = $("#servPriv").val();
    if (error) {
      updateBtn(true);
      return;
    }
    if ('Y' == isPAgreementPay) {   //如果为协议支付，不需要进行实名认证,手机号直接取默认值
      //_$("#mobileId").sgAttr.attr({ t : "value", v : mobileObj.val() });
      mobileId = mobileObj.val();
      $("#verifyCode").val(smsVerifyCode.val());
    } else {
      // 校验支付要素
      error = checkElementAndSetRsa();
      if (error) {
        updateBtn(true);
        return;
      }
      //设置隐藏域中支付要素的值
      if (!("B007" == binBankId)) {//非招行，生成身份证号的加密串
        _$("#identityCode").sgAttr.attr({ t: "value", v: identityCode });
        _$("#cardHolder").sgAttr.attr({ t: "value", v: cardHolder });
      }
      _$("#cvv2").sgAttr.attr({ t: "value", v: cvv2 });
      _$("#cardId").sgAttr.attr({ t: "value", v: cardId });
      _$("#mobileId").sgAttr.attr({ t: "value", v: mobileId });
      $("#validDateY").val(validDateY);
      $("#validDateM").val(validDateM);
      $("#verifyCode").val(smsVerifyCode.val());
    }
  } catch (e) {
    alert(e);
    updateBtn(true);
    return;
  }

  var ajaxSignSource = $("#ajaxSignSource").val();
  var url = _$("#shortCutAjaxVerifyCodeValidateUrl").sgAttr.attr({ t: "value" });
  // 验证短信验证url
  url = url + "?rpid=" + rpid + "&verifyCode=" + smsVerifyCode.val()
    + "&mobileId=" + mobileId + "&binBankId=" + binBankId
    + "&cardId=" + encodeURIComponent(cardId) + "&identityCode=" + encodeURIComponent(identityCode) + "&cvv2="
    + encodeURIComponent(cvv2) + "&validDateM=" + validDateM + "&validDateY=" + validDateY + "&cardHolder=" + encodeURIComponent(cardHolder)
    + "&trace=" + trace + "&instId=" + instId + "&cardType=1" + "&isPAgreementPay=" + isPAgreementPay + "&ajaxSignSource="
    + ajaxSignSource + "&pAgreementId=" + pAgreementId + "&ubAgreementId=" + ubAgreementId + "&productId=" + productId + "&bProductId=" + bProductId +
    "&baseProductId=" + baseProductId + "&merCustId=" + merCustId + "&servPriv=" + servPriv + "&merId=" + $("#merId").val() + "&orderId=" + $("#orderId").val() + "&orderDate=" + $("#orderDate").val();
  // 验证通过，请求后台，请求参数 短信验证码,手机号,binBankId
  _$().reqAjax(url, function (payResult) {
    var backInf = eval('(' + payResult + ')');
    if (backInf != null && backInf.retCode == "0000") { // 短信验证码校验成功
      $("#verifyCodeErr").hide();
      // 短信验证成功，提交确认支付表单
      var url = _$("#shortCutPayUrl").sgAttr.attr({ t: "value" });
      //判断是否注册支付协议
      var isRegPAagreementPay = $("#isChecked").attr("checked");
      if (true == isRegPAagreementPay) {
        $("#isRegAgreement").val("1");
      } else {
        $("#isRegAgreement").val("0");
      }
      //$("#upayShortCutPayForm").attr("action", url);
      //document.upayShortCutPayForm.submit();
      var instId = $("#instId").val(), upayPriv = $("#upayPriv").val();
      var isRegAgreement = $("#isRegAgreement").val(), bankName = $("#bankName").val(), merCustId = $("#merCustId").val();
      var payDate = $("#payDate").val(), merId = $("#merId").val(), subMerId = $("#subMerId").val(), splitData = $("#splitData").val();
      var channelId = $("#channelId").val(), orderId = $("#orderId").val(), orderDate = $("#orderDate").val(), payType = $("#payType").val();
      var merCheckDate = $("#merCheckDate").val(), amount = $("#amount").val(), oriAmount = $("#oriAmount").val();
      var bankId = $("#bankId").val(), retUrl = $("#retUrl").val(), notifyUrl = $("#notifyUrl").val(), userMerIp = $("#userMerIp").val();
      var areaCode = $("#areaCode").val(), splitType = $("#splitType").val(), splitCategory = $("#splitCategory").val();
      var splitDataLink = $("#splitDataLink").val(), preComAmt = $("#preComAmt").val(), isRegAgreement = $("#isRegAgreement").val();
      var priorityType = $("#priorityType").val(), signSource = $("#signSource").val();
      url = url + "?rpid=" + rpid + "&instId=" + instId + "&trace=" + trace + "&upayPriv=" + upayPriv + "&binBankId=" + binBankId
        + "&isRegAgreement=" + isRegAgreement + "&bankName=" + encodeURIComponent(bankName) + "&payDate=" + payDate
        + "&merId=" + merId + "&subMerId=" + subMerId + "&splitData=" + splitData + "&channelId=" + channelId
        + "&orderId=" + orderId + "&orderDate=" + orderDate + "&payType=" + payType + "&merCheckDate=" + merCheckDate
        + "&productId=" + productId + "&amount=" + amount + "&oriAmount=" + oriAmount + "&mobileId=" + mobileId
        + "&retUrl=" + retUrl + "&notifyUrl=" + notifyUrl + "&userMerIp=" + userMerIp + "&areaCode=" + areaCode
        + "&merCustId=" + merCustId + "&splitType=" + splitType + "&splitCategory=" + splitCategory + "&splitDataLink=" + splitDataLink
        + "&isRegAgreement=" + isRegAgreement + "&pAgreementId=" + pAgreementId + "&priorityType=" + priorityType
        + "&ubAgreementId=" + ubAgreementId + "&baseProductId=" + baseProductId + "&bProductId=" + bProductId
        + "&servPriv=" + servPriv + "&cardId=" + encodeURIComponent(cardId) + "&identityCode="
        + encodeURIComponent(identityCode) + "&cvv2=" + encodeURIComponent(cvv2) + "&validDateM=" + validDateM + "&validDateY=" + validDateY
        + "&cardHolder=" + encodeURIComponent(cardHolder) + "&isPAgreementPay=" + isPAgreementPay + "&verifyCode=" + smsVerifyCode.val()
        + "&signSource=" + signSource + "&trxtrmtpPay=" + $("#trxtrmtpPay").val() + "&trxtrmnoPay=" + $("#trxtrmnoPay").val() + "&trxtrmtpRecv="
        + $("#trxtrmtpRecv").val() + "&trxtrmnoRecv=" + $("#trxtrmnoRecv").val() + "&equipIp=" + $("#equipIp").val() + "&equipMac=" + $("#equipMac").val()
        + "&equipImei=" + $("#equipImei").val() + "&equipImsi=" + $("#equipImsi").val() + "&equipIccid=" + $("#equipIccid").val() + "&equipWifimac="
        + $("#equipWifimac").val() + "&equipGps=" + $("#equipGps").val() + "&platformName=" + $("#platformName").val();
      _$().reqAjax(url, function (result) {
        var payResultInf = eval('(' + result + ')');
        if (payResultInf != null && payResultInf.pageRetCode == 0) {
          var returnUrl = payResultInf.retUrl;
          window.location = returnUrl;
        }
        else if (payResultInf.trace != null && payResultInf.trace != "") {
          trace = payResultInf.trace;
          signSource = payResultInf.signSource;
          _$("#trace").sgAttr.attr({ t: "value", v: payResultInf.trace });
          $("#signSource").val(payResultInf.signSource);
          updateBtn(true);
          $("#verifyCodeV").attr("value", "");
          var showMsg = payResultInf.pageRetMsg + "<br>\u518D\u6B21\u652F\u4ED8\u8BF7\u91CD\u65B0\u83B7\u53D6\u77ED\u4FE1\u9A8C\u8BC1\u7801\uFF01";
          $("#msgVerifyErr>span").html(showMsg);
          $("#msgVerifyErr").show();
          $("#verifyCodeErr").hide();
          wait = 0;
        } else if (payResultInf.isTraceUsed == "0") {
          updateBtn(true);
          var showMsg = payResultInf.pageRetMsg;
          $("#msgVerifyErr>span").html(showMsg);
          $("#msgVerifyErr").show();
          $("#verifyCodeErr").hide();
        } else {
          var pageRetCode = payResultInf.pageRetCode;
          var pageRetMsg = payResultInf.pageRetMsg;
          window.location = "common/500.jsp?pageRetCode=" + pageRetCode + "&pageRetMsg=" + pageRetMsg;
        }
      });
    } else { // 短信验证码校验失败
      updateBtn(true);
      $("#msgVerifyErr>span").html(backInf.retMsg);
      $("#msgVerifyErr").show();
      $("#verifyCodeErr").hide();
    }
  });
}

var TINY = {};
function T$(i) {
  return document.getElementById(i);
}

TINY.box = function () {
  var q, p, m, b, fn, ic, iu, iw, ih, ia, f = 0;
  return {
    show: function (c, u, w, h, a, t) {
      if (!f) {

        p = document.createElement('div');
        p.id = 'tinybox';
        m = document.createElement('div');
        m.id = 'tinymask';
        b = document.createElement('div');
        b.id = 'tinycontent';
        e = document.createElement('div');
        e.id = 'close';
        q = document.createElement('div');
        q.id = 'titletop';
        // f=document.createElement('div'); f.id='close1';
        document.body.appendChild(m);
        document.body.appendChild(p);
        p.appendChild(b);
        p.appendChild(e);
        // p.appendChild(f);
        e.onclick = TINY.box.hide;
        // f.onclick=TINY.box.hide;
        window.onresize = TINY.box.resize;
        f = 1;
      }
      if (!a && !u) {
        p.style.width = w ? w + 'px' : 'auto';
        p.style.height = h ? h + 'px' : 'auto';
        p.style.backgroundImage = 'none';
        b.innerHTML = c;
      } else {
        b.style.display = 'none';
        p.style.width = p.style.height = '0px';
      }
      this.mask();
      ic = c;
      iu = u;
      iw = w;
      ih = h;
      ia = a;
      this.alpha(m, 1, 15, 3);
      if (t) {
        setTimeout(function () {
          TINY.box.hide();
        }, 500 * t);
      }
    },
    fill: function (c, u, w, h, a) {
      if (u) {
        p.style.backgroundImage = '';
        var x = window.XMLHttpRequest ? new XMLHttpRequest()
          : new ActiveXObject('Microsoft.XMLHTTP');
        x.onreadystatechange = function () {
          if (x.readyState == 4 && x.status == 200) {
            TINY.box.psh(x.responseText, w, h, a);
          }
        };
        x.open('GET', c, 1);
        x.send(null);
      } else {
        this.psh(c, w, h, a);
      }
    },
    psh: function (c, w, h, a) {
      if (a) {
        if (!w || !h) {
          var x = p.style.width, y = p.style.height;
          b.innerHTML = c;
          p.style.width = w ? w + 'px' : '';
          p.style.height = h ? h + 'px' : '';
          b.style.display = '';
          w = parseInt(b.offsetWidth);
          h = parseInt(b.offsetHeight);
          b.style.display = 'none';
          p.style.width = x;
          p.style.height = y;
        } else {
          b.innerHTML = c;
        }
        this.size(p, w, h, 4);
      } else {
        p.style.backgroundImage = 'none';
      }
    },
    hide: function () {
      TINY.box.alpha(p, -1, 0, 5);
    },
    resize: function () {
      TINY.box.pos();
      TINY.box.mask();
    },
    mask: function () {
      m.style.height = TINY.page.theight() + 'px';
      m.style.width = '';
      m.style.width = TINY.page.twidth() + 'px';
    },
    pos: function () {
      var t = (TINY.page.height() / 2) - (p.offsetHeight / 2);
      t = t < 10 ? 10 : t;
      p.style.top = (t + TINY.page.top()) + 'px';
      p.style.left = (TINY.page.width() / 2) - (p.offsetWidth / 2) + 'px';
    },
    alpha: function (e, d, a, s) {
      clearInterval(e.ai);
      if (d == 1) {
        e.style.opacity = 0;
        e.style.filter = 'alpha(opacity=0)';
        e.style.display = 'block';
        this.pos();
      }
      e.ai = setInterval(function () {
        TINY.box.twalpha(e, a, d, s);
      }, 20);
    },
    twalpha: function (e, a, d, s) {
      var o = Math.round(e.style.opacity * 40);

      if (o == a) {
        clearInterval(e.ai);
        if (d == -1) {
          e.style.display = 'none';
          e == p ? TINY.box.alpha(m, -1, 0, 3)
            : b.innerHTML = p.style.backgroundImage = '';
        } else {
          e == m ? this.alpha(p, 1, 100, 5) : TINY.box.fill(ic, iu,
            iw, ih, ia);

        }
      } else {
        var n = o + Math.ceil(Math.abs(a - o) / s) * d;
        e.style.opacity = n / 40;
        e.style.filter = 'alpha(opacity=' + n + ')';
      }
    },
    size: function (e, w, h, s) {

      e = typeof e == 'object' ? e : T$(e);
      clearInterval(e.si);
      var ow = e.offsetWidth, oh = e.offsetHeight, wo = ow
        - parseInt(e.style.width), ho = oh
          - parseInt(e.style.height);
      var wd = ow - wo > w ? -1 : 1, hd = (oh - ho > h) ? -1 : 1;
      e.si = setInterval(function () {
        TINY.box.twsize(e, w, wo, wd, h, ho, hd, s);
      }, 20);

    },
    twsize: function (e, w, wo, wd, h, ho, hd, s) {
      var ow = e.offsetWidth - wo, oh = e.offsetHeight - ho;
      if (ow == w && oh == h) {
        clearInterval(e.si);
        p.style.backgroundImage = 'none';
        b.style.display = 'block';
      } else {
        if (ow != w) {
          e.style.width = ow + (Math.ceil(Math.abs(w - ow) / s) * wd)
            + 'px';
        }
        if (oh != h) {
          e.style.height = oh
            + (Math.ceil(Math.abs(h - oh) / s) * hd) + 'px';
        }
        this.pos();
      }
    }
  };
}();

TINY.page = function () {
  return {
    top: function () {
      return document.body.scrollTop
        || document.documentElement.scrollTop;
    },
    width: function () {
      return self.innerWidth || document.documentElement.clientWidth;
    },
    height: function () {
      return self.innerHeight || document.documentElement.clientHeight;
    },
    theight: function () {
      var d = document, b = d.body, e = d.documentElement;
      return Math.max(Math.max(b.scrollHeight, e.scrollHeight), Math.max(
        b.clientHeight, e.clientHeight));
    },
    twidth: function () {
      var d = document, b = d.body, e = d.documentElement;
      return Math.max(Math.max(b.scrollWidth, e.scrollWidth), Math.max(
        b.clientWidth, e.clientWidth));
    }
  };
}();
